package com.mc.read.pcApi.service;

import com.mc.read.dao.mapper.ClickpluginfunctionMapper;
import com.mc.read.dao.mapper.UserbookMapper;
import com.mc.read.dao.models.Bookinformation;
import com.mc.read.dao.models.Clickpluginfunction;
import com.mc.read.dao.models.Userbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/18 11:43
 */
@Service
public class FunctionService {
    @Autowired
    ClickpluginfunctionMapper clickpluginfunctionMapper;
    @Autowired
    UserbookMapper userbookMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 更新功能点击次数
     **/
    public void updateFunctionClick(String functionName) {

        int i = clickpluginfunctionMapper.updateByFunctionName(functionName);
        if (i == 1) {
            return;
        }
        if (i == 0) {
            Clickpluginfunction clickpluginfunction = new Clickpluginfunction();
            clickpluginfunction.setClickday(new Date());
            clickpluginfunction.setClickcount(1);
            clickpluginfunction.setFuncationname(functionName);
            clickpluginfunctionMapper.insertSelective(clickpluginfunction);
        } else {
            List<Clickpluginfunction> list = clickpluginfunctionMapper.selectByFunctionName(functionName);
            for (int k = 0; k < list.size(); k++) {
                if (k != 0) {
                    clickpluginfunctionMapper.deleteByPrimaryKey(list.get(k).getClickpluginfunctionid());
                }
            }
        }
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 更新用户阅读的书籍 updateUserBook
     **/
    public void updateUserBook(Integer userid, Bookinformation bookinformation) {
        Userbook userbook = new Userbook();
        userbook.setBookid(bookinformation.getBookinformationid());
        userbook.setUserid(userid);
        int i = userbookMapper.updateUserBook(userbook);
        if (i == 1) {
            return;
        }
        if (i == 0) {
            userbook.setReadsum(1);
            userbook.setBookname(bookinformation.getBookname());
            userbook.setBooktypeid(bookinformation.getMainlevelid());
            userbook.setBooktypename(bookinformation.getMainlevelname());
            userbookMapper.insertSelective(userbook);
        } else {
            List<Userbook> list = userbookMapper.selectByUserBook(userbook);
            for (int k = 0; k < list.size(); k++) {
                if (k != 0) {
                    userbookMapper.deleteByPrimaryKey(list.get(k).getUserbookid());
                }
            }
        }
    }

}
